'use strict';
// 挂号接口 RegistrationController
const { Controller } = require('egg');

class RegistrationController extends Controller {
  // 查找所有挂号预约
  async findAllRegistration () {
    const { ctx } = this;
    let Op = this.app.Sequelize.Op
    // ctx.request.body  post的参数
    const data = ctx.request.body;
    console.log(data, "data");
    // 查询数据库
    const _ctx = this.ctx;
    const Registration = await _ctx.model.Registration.findAll({
      where: {
        time_registration: {
          [Op.lt]: new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1),
          [Op.gt]: new Date(new Date().setHours(0, 0, 0, 0)),
        },
      },
      include: [
        {
          model: ctx.model.Patient
        },
      ],
    });
    ctx.body = { code: 0, data: Registration }
  }


  // 获取当月，每天的挂号数
  async findCountByDateRegistration () {
    const { ctx } = this;
    let Op = this.app.Sequelize.Op
    let fn = this.app.Sequelize.fn
    let col = this.app.Sequelize.col
    let where = this.app.Sequelize.where
    // ctx.request.body  post的参数
    const data = ctx.request.body;
    console.log(data, "data");
    // 查询数据库
    const _ctx = this.ctx;
    const Registration = await _ctx.model.Registration.findAll({
      attributes: [
        [fn('date_format', col('time_registration'), '%m-%d'), 'date'],
        [fn('count', col('*')), 'total'],
      ],
      group: col('date'),
      order: [[col('date'), 'asc']],
      where: where(fn('date_format', col('time_registration'), '%Y-%m'), fn('date_format', new Date(), '%Y-%m'))
    });
    ctx.body = { code: 0, data: Registration }
  }

  // 获取就诊与待就诊与已就诊 数量
  async getRegistrationCount () {
    const { ctx } = this;
    let Op = this.app.Sequelize.Op
    // ctx.request.body  post的参数
    const data = ctx.request.body;
    console.log(data, "data");
    // 查询数据库
    const _ctx = this.ctx;
    const Registration = await _ctx.model.Registration.count({
      where: {
        // [Op.and]: [{ a: 5 }, { b: 6 }]
        time_registration: {
          [Op.lt]: new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1),
          [Op.gt]: new Date(new Date().setHours(0, 0, 0, 0)),
        },
      }
    });
    // 待就诊
    const WaitRegistration = await _ctx.model.Registration.count({
      where: {
        [Op.and]: [{
          time_registration: {
            [Op.lt]: new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1),
            [Op.gt]: new Date(new Date().setHours(0, 0, 0, 0)),
          }
        }, { state_registration: 0 }]

      }
    });
    // 已就诊
    // 待就诊
    const AlreadyRegistration = await _ctx.model.Registration.count({
      where: {
        [Op.and]: [{
          time_registration: {
            [Op.lt]: new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1),
            [Op.gt]: new Date(new Date().setHours(0, 0, 0, 0)),
          }
        }, { state_registration: 1 }]
      }
    });

    ctx.body = { code: 0, data: { all_registration: Registration, wait_wegistration: WaitRegistration, already_registration: AlreadyRegistration, } }
  }


  // 通过ID查找挂号预约
  async findRegistrationByIdRegistration () {
    const { ctx } = this;
    let Op = this.app.Sequelize.Op
    const data = ctx.request.body;
    const _ctx = this.ctx;
    const appointment = await _ctx.model.Registration.findOne({
      where: {
        id_registration: data.id
      }, include: [
        {
          model: ctx.model.Patient
        },
        {
          model: ctx.model.Case
        },
      ],
    })
    ctx.body = { code: 0, data: appointment }
  }



  // 添加挂号预约
  async addRegistration () {
    const { ctx } = this;
    const { data } = ctx.request.body;
    const _ctx = this.ctx;
    // 创建病人信息
    const Patient = await _ctx.model.Patient.create({ name_patient: data.name_patient, phone_patient: data.phone_patient, birth_patient: data.birth_patient, age_patient: data.age_patient, identity_patient: data.identity_patient, sex_patient: data.sex_patient })
    const id_patient = Patient.dataValues.id_patient

    const Case = await _ctx.model.Case.create()

    // 创建就诊信息
    const Registration = await _ctx.model.Registration.create({
      id_patient: id_patient, time_registration: data.time_registration, id_case: Case.id_case,
      // mock医生
      id_user: "00001", state_registration: "0",
    })
    ctx.body = { code: 0, data: { Registration, Patient, } }
  }

  // 查询就诊数据，通过一些选项
  async findAllByOptionRegistration () {
    const { ctx } = this;
    let Op = this.app.Sequelize.Op
    const { data } = ctx.request.body;
    console.log(data, "data");
    const _ctx = this.ctx;
    /*   data: {
    state_registration: null,
    time_registration: null,
    time_registration_last: null,
    name_patient: null
  } */
    // 待就诊 数字格式化
    if (data.state_registration == '待就诊') {
      data.state_registration = 0
    } else if (data.state_registration == "已就诊") {
      data.state_registration = 1
    } else {
      data.state_registration = null
    }
    if (data.state_registration !== null && data.time_registration !== null && data.time_registration_last !== null && data.name_patient !== null) {
      // 传递所有参数
      const Registration = await _ctx.model.Registration.findAll({
        where: {
          time_registration: {
            [Op.lt]: data.time_registration_last,
            [Op.gt]: data.time_registration,
          },
          state_registration: data.state_registration
        },
        include: [
          {
            model: ctx.model.Patient,
            where: {
              name_patient: {
                [Op.like]: data.name_patient
              }
            }
          },]
      });
      ctx.body = { code: 0, data: Registration }
    }
    else if (data.state_registration == null && data.time_registration && data.time_registration_last && data.name_patient) {
      // 传递所有参数
      const Registration = await _ctx.model.Registration.findAll({
        where: {
          time_registration: {
            [Op.lt]: data.time_registration_last,
            [Op.gt]: data.time_registration,
          },
        },
        include: [
          {
            model: ctx.model.Patient,
            where: {
              name_patient: {
                [Op.like]: data.name_patient
              }
            }
          },]
      });
      ctx.body = { code: 0, data: Registration }
    } else if (data.state_registration == null && data.time_registration == null && data.time_registration_last == null && data.name_patient) {
      const Registration = await _ctx.model.Registration.findAll({
        include: [
          {
            model: ctx.model.Patient,
            where: {
              name_patient: {
                [Op.like]: data.name_patient
              }
            }
          },]
      });
      ctx.body = { code: 0, data: Registration }
    }
    else {
      // 没有传递任何参数
      const Registration = await _ctx.model.Registration.findAll({
        // 一个月内的所有 
        where: {
          time_registration: {
            [Op.lt]: new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1),
            [Op.gt]: new Date(new Date().setHours(0, 0, 0, 0) - 30 * 24 * 60 * 60 * 1000 - 1),// 起始时间
          }
        },
        include: [
          {
            model: ctx.model.Patient
          }]
      });
      ctx.body = { code: 0, data: Registration }
    }
  }
}

module.exports = RegistrationController;
